//使用友元（friend）函数方式，实现了 —（减法）、>>、<<重载
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex(double r=0,double i=0):re(r),im(i) {}

    friend Complex operator-(Complex cm1,Complex cm2);
    friend istream &operator >> (istream &in, Complex &cm);
    friend ostream &operator<<(ostream &out, Complex cm);

private:
    double re;//实部
    double im; //虚部
};

Complex operator-(Complex cm1,Complex cm2)
{
    Complex cm;
    cm.re = cm1.re - cm2.re;
    cm.im = cm1.im - cm2.im;
    return cm;
}
ostream &operator<<(ostream &out, Complex cm)
{
    out << cm.re << "+" << cm.im << "i" ;
    return out;
}

istream &operator >>(istream &in, Complex &cm)
{
    in >> cm.re >> cm.im;
    return in;
}

int main()
{
    Complex cm1(1.5,-2.3),cm2(3.3,8.7),cm3,cm4;
    cm3 = cm2 - cm1;
    cout<<"cm3="<<cm3<<endl;
    cin>>cm4;
    cout<<"cm4="<<cm4<<endl;
    return 0;
}
